package com.cscp.javastudy.juc.lock;

import java.util.concurrent.TimeUnit;

/**
 * 8锁，关于锁的八个问题
 */
public class LockDemo1 {

    public static void main(String[] args) {
        Phone phone = new Phone();

        new Thread(() -> {
            phone.sendSMS();
        }, "A").start();

        //捕获
        try {
            TimeUnit.SECONDS.sleep(1);
            System.out.println("我睡了1s");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            phone.call();
        }, "B").start();
    }

}

/**
 * 资源类
 */
class Phone {

    /*
     * synchronized 锁的对象是方法的调用者
     * 两个方法用的是同一个锁，谁先拿到谁执行
     */
    public synchronized void sendSMS(){
        //捕获
        try {
            TimeUnit.SECONDS.sleep(3);
            System.out.println("我睡了3s");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public synchronized void call(){
        System.out.println("打电话");
    }

}
